package com.test2015.Swing.popup.pf;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.StringTokenizer;

import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicToolTipUI;

public class ComponentToolTipUI extends BasicToolTipUI
{
    /**
     * The single shared UI instance.
     **/
    static ComponentToolTipUI sharedInstance = new ComponentToolTipUI();
    /**
     * The margin around the text.
     **/
    static final int MARGIN = 2;
    /**
     * The character to use in the StringTokenizer to
     * separate lines in the ToolTip. This could be the
     * system property end of line character.
     **/
    static final String lineSeparator = "\n";
    /**
     * MultiLineToolTipUI, constructor.
     * <p>
     * Have the constructor be protected so we can be subclassed,
     * but not created by client classes.
     **/
    protected ComponentToolTipUI()
    {
        super();
    }
    /**
     * Create the UI component for the given component.
     * The same UI can be shared for all components, so
     * return our shared instance.
     * <p>
     * @param c The component to create the UI for.
     * @return Our shared UI component instance.
     **/
    public static ComponentUI createUI(JComponent c)
    {
        return sharedInstance;
    }
    /**
     * Paint the ToolTip. Use the current font and colors
     * set for the given component.
     * <p>
     * @param g The graphics to paint with.
     * @param c The component to paint.
     **/
    public void paint(Graphics g, JComponent c)
    {
  // Paints each of the components in this container.
  if(c.getComponentCount() > 0) {
          c.paintComponents(g);
          return;
  }
  // If no components, then paint a muiltiple line tooltip

  //
        // Determine the size for each row.
        //
        Font font = c.getFont();
        FontMetrics fontMetrics = c.getFontMetrics(font);
        int fontHeight = fontMetrics.getHeight();
        int fontAscent = fontMetrics.getAscent();
        //
        // Paint the background in the tip color.
        //
        g.setColor(c.getBackground());
        Dimension size = c.getSize();
        g.fillRect(0, 0, size.width, size.height);
        //
        // Paint each line in the tip using the
        // foreground color. Use a StringTokenizer
        // to parse the ToolTip. Each line is left
        // justified, and the y coordinate is updated
        // through the loop.
        //
        g.setColor(c.getForeground());
        int y = 2+fontAscent;
        String tipText = ((JToolTip)c).getTipText();
        StringTokenizer tokenizer = new StringTokenizer(tipText, lineSeparator);
        int numberOfLines = tokenizer.countTokens();
        for (int i = 0; i < numberOfLines; i++)
        {
            g.drawString(tokenizer.nextToken(), MARGIN, y);
            y += fontHeight;
        }
    }
    // paint
    /**
     * The preferred size for the ToolTip is the width of
     * the longest row in the tip, and the height of a
     * single row times the number of rows in the tip.
     *
     * @param c The component whose size is needed.
     * @return The preferred size for the component.
     **/
    public Dimension getPreferredSize(JComponent c)
    {
        // If has children components
  if(c.getComponentCount() > 0) {
         return c.getLayout().preferredLayoutSize(c);
  }
  //
        // Determine the size for each row.
        //
        Font font = c.getFont();
        FontMetrics fontMetrics = c.getFontMetrics(font);
        int fontHeight = fontMetrics.getHeight();
        //
        // Get the tip text string.
        //
        String tipText = ((JToolTip)c).getTipText();
        //
        // Empty tip, use a default size.
        //
        if (tipText == null)
            return new Dimension(2 * MARGIN, 2 * MARGIN);
        //
        // Create a StringTokenizer to parse the ToolTip.
        //
        StringTokenizer tokenizer = new StringTokenizer(tipText, lineSeparator);
        int numberOfLines = tokenizer.countTokens();
        //
        // Height is number of lines times height of a single line.
        //
        int height = numberOfLines * fontHeight;
        //
        // Width is width of longest single line.
        //
        int width = 0;
        for (int i = 0; i < numberOfLines; i++)
        {
            int thisWidth = fontMetrics.stringWidth(tokenizer.nextToken());
            width = Math.max(width, thisWidth);
        }
        //
        // Add the margin to the size, and return.
        //
        return (new Dimension(width + 2 * MARGIN, height + 2 * MARGIN));
    }
    // getPreferredSize
}